import {
	addCurrencyRate,
	deleteCurrencyRate,
	getCnyByCurrent,
	getCurrencyRate,
	queryCurrencyRate,
	updateCurrencyRate
} from "../../services/quotationSupport/currencyRate";
import Constants from '../../utils/Constants'
import {message} from "antd";

const R = require('ramda');


export default {
	namespace: 'currencyRate',
	state: {
		page: {},
		list: [],
		dataSource: {},
	},
	reducers: {
        /**
         * 刷新state
         *
         */
		refreshState(state, { payload }) {
			return { ...state, ...payload };
		}
	},
	effects: {
        /**
         * 分页查询汇率
         *
         * @param call
         * @param put
         */
		*queryCurrencyRate({ payload }, { call, put }) {
			const res = yield call(queryCurrencyRate, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				yield put({
					type: 'refreshState',
					payload: {
						list: res.data.content.list,
						page: res.data.content.page
					}
				});
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

        /**
         * 添加汇率
         *
         * @param call
         */
		*addCurrencyRate({ payload }, { call }) {
			const res = yield call(addCurrencyRate, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				message.info("汇率新增成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

        /**
         * 删除汇率
         *
         * @param payload
         * @param call
         * @param put
         */
		*deleteCurrencyRate({ payload }, { call }) {
			const res = yield call(deleteCurrencyRate, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				message.info("汇率删除成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

        /**
         * 修改汇率
         *
         * @param call
         * @param put
         * @param select
         */
		*updateCurrencyRate({ payload }, { call, put }) {
			const res = yield call(updateCurrencyRate, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				message.info("汇率更新成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

        /**
         * 获取汇率
         *
         * @param payload
         * @param call
         * @param put
         */
		*getCurrencyRate({ payload }, { call, put }) {
			if (payload.id) {
				const res = yield call(getCurrencyRate, { ...R.omit(['onSuccess', 'onFail'])(payload) });
				if (res && res.data.resultCode === "ok") {
					yield put({
						type: 'refreshState',
						payload: {
							dataSource: res.data.content
						}
					});
				} else {
					if (payload.onFail) payload.onFail();
				}
			} else {
				yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
			}
		},
		/**
         * 币种汇率转人民币
         *
         * @param call
         */
		*getCnyByCurrent({ payload }, { call, put }) {
			const res = yield call(getCnyByCurrent, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				yield put({
					type: 'refreshState',
					payload: {
						dataSource: res.data.content
					}
				});
			} else {
				yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
			}
		},
	},
	subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};
